package com.ogilvy.audi.webapp.action;

import java.util.List;

import com.ogilvy.audi.model.CircleUserExt;
import com.ogilvy.audi.service.CircleUserExtManager;
import com.opensymphony.xwork2.Preparable;

public class CircleUserExtAction extends BaseAction implements Preparable {
    
    /**
     * 
     */
    private static final long serialVersionUID = -802430177015836165L;
    
    private CircleUserExtManager circleUserExtManager;
    private List circleUserExts;
    private CircleUserExt circleUserExt;
    private Long seqid;
    private String query;

    
    public void setCircleUserExtManager(CircleUserExtManager circleUserExtManager) {
        this.circleUserExtManager = circleUserExtManager;
    }

    public List getCircleUserExts() {
        return circleUserExts;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleUserExtId = getRequest().getParameter("circleUserExt.seqid");
            if (circleUserExtId != null && !circleUserExtId.equals("")) {
                circleUserExt = circleUserExtManager.get(new Long(circleUserExtId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        circleUserExts = circleUserExtManager.search(query, CircleUserExt.class);
        return SUCCESS;
    }

    public void setSeqid(Long seqid) {
        this.seqid = seqid;
    }

    public CircleUserExt getCircleUserExt() {
        return circleUserExt;
    }

    public void setCircleUserExt(CircleUserExt circleUserExt) {
        this.circleUserExt = circleUserExt;
    }

    public String delete() {
        circleUserExtManager.remove(circleUserExt.getSeqid());
        saveMessage(getText("circleUserExt.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (seqid != null) {
            circleUserExt = circleUserExtManager.get(seqid);
        } else {
            circleUserExt = new CircleUserExt();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleUserExt.getSeqid() == null);

        circleUserExtManager.save(circleUserExt);

        String key = (isNew) ? "circleUserExt.added" : "circleUserExt.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}